package com.ky.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.course.entity.Comment;
import com.ky.common.param.CommentAdd;
import com.ky.common.param.CourseCommentVo;
import com.ky.common.vo.R;
import com.ky.course.mapper.CommentMapper;
import com.ky.course.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zpa
 * @since 2024-06-22
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Autowired
    public CommentMapper commentMapper;

    @Override
    public List<CourseCommentVo> getCommentList(Integer courseId) {
        if (courseId==null){
            return null;
        }
        List<CourseCommentVo> commentList = commentMapper.getCommentList(courseId);
        return commentList;
    }

    /**
     * 提交课程评论
     * @param id
     * @param commentAdd
     * @return
     */
    @Override
    public R submitComment(Integer id, CommentAdd commentAdd) {
        if (commentAdd.getCourseId()==null){
            return R.fail("课程号不能未空");
        }
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",commentAdd.getCourseId())
                .eq("user_id",id);
        List<Comment> comments = commentMapper.selectList(wrapper);
        if (comments!=null && !comments.isEmpty()){
            return R.fail("您已评论");
        }

        Comment comment = new Comment();
        comment.setContent(commentAdd.getContent())
                .setUserId(id)
                .setCourseId(commentAdd.getCourseId())
                .setCreateTime(new Date());

        int result = commentMapper.insert(comment);
        return result>0?R.ok():R.fail("评论失败");
    }
}
